package com.chatim.nonamechatim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chatim.nonamechatim.mapper.UserMapper;
import com.chatim.nonamechatim.pojo.User;
import com.chatim.nonamechatim.pojo.UserApply;
import com.chatim.nonamechatim.pojo.UserFriend;
import com.chatim.nonamechatim.service.MessageService;
import com.chatim.nonamechatim.service.UserApplyService;
import com.chatim.nonamechatim.service.UserFriendService;
import com.chatim.nonamechatim.service.UserService;
import com.chatim.nonamechatim.vo.UserSearchVo;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserDetailsService, UserService{

    @Resource
    UserMapper userMapper;

    @Resource
    UserFriendService userFriendService;

    @Resource
    UserApplyService userApplyService;

    @Resource
    MessageService messageService;

    /**
     * 用户鉴权
     * @param username the username identifying the user whose data is required.
     * @return 查询到的用户
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if(user == null) throw new UsernameNotFoundException("用户不存在");
        return user;
    }

    /**
     * 用户注册
     * @param user 用户信息
     * @return boolean
     */
    @Override
    public Boolean register(User user) {
        user.setAvatar("01");
        user.setPassword("{noop}"+user.getPassword());
        return baseMapper.insertOrUpdate(user);
    }

    /**
     * 模糊查询非好友用户
     * @param key 关键词
     * @return 用户列表
     */
    @Override
    public List<UserSearchVo> fuzzyQueryByAddUser(Long uid, String key) {
        return userMapper.fuzzyQueryByAddUser(uid, key);
    }

    @Transactional
    @Override
    public Boolean agreeApply(Long id, Long fid) {
        // 将好友申请设置为通过
        LambdaUpdateWrapper<UserApply> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserApply::getTargetId, id).eq(UserApply::getUid, fid).set(UserApply::getStatus, 2);
        userApplyService.update(updateWrapper);
        // 更新好友关联表
        userFriendService.saveBatch(Arrays.asList(
                new UserFriend(id, fid),
                new UserFriend(fid, id)
        ));
        // 更新会话列表
        messageService.addContact(id, fid);
        return true;
    }
}
